package com.jdk8.demo.functional.function;

import java.util.Arrays;
import java.util.List;
import java.util.function.Function;

/**
 * 函数型接口
 * 参数：R
 *方法：apply(T t);
 */
public class FunctionTest {

    public static void main(String[] args) {
        FunctionTest functionTest = new FunctionTest();
        List<String> strings = Arrays.asList("Hello", "world", "Hello World");

        strings.forEach(str -> System.out.println(str.toUpperCase()));

        strings.stream().map(String::toUpperCase).forEach(System.out::println);


        functionTest.operation(2, (Integer num) -> {
            return 2 + 3;
        });

        Integer composeTest = functionTest.composeTest(3, num -> {
            System.out.println("compose");
            return num + 1;
        });
        Integer andThenTest = functionTest.andThenTest(3, num -> {
            System.out.println("addThen");
            return num + 1;
        });
    }


    public Integer operation(Integer num, Function<Integer, Integer> behavior) {
        return behavior.apply(num);

    }

    public Integer composeTest(Integer num, Function<Integer, Integer> behavior) {
        return behavior.compose((Integer num2) -> {
            System.out.println("composeTest");
            return num2 + 3;
        }).apply(num);
    }

    public Integer andThenTest(Integer num, Function<Integer, Integer> behavior) {
        return behavior.andThen((Integer num2) -> {
            System.out.println("andThenTest");
            return num2 + 3;
        }).apply(num);
    }


}